package com.swz.dcrm.ui.mine;


import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.navigation.fragment.NavHostFragment;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bigkoo.pickerview.TimePickerView;
import com.flyco.dialog.listener.OnOperItemClickL;
import com.flyco.dialog.widget.ActionSheetDialog;
import com.swz.dcrm.model.BaseResponse;
import com.swz.dcrm.model.Dictionary;
import com.swz.dcrm.model.Schedule;
import com.swz.dcrm.ui.base.BaseFragment;
import com.swz.dcrm.R;

import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.http.Body;

import com.swz.dcrm.R;
import com.swz.dcrm.ui.viewmodel.MainViewModel;
import com.swz.dcrm.ui.viewmodel.ScheduleViewModel;
import com.swz.dcrm.util.Constant;
import com.swz.dcrm.util.DateUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;

/**
 * A simple {@link Fragment} subclass.
 */
public class EditScheduleFragment extends BaseFragment {

    @BindView(R.id.toolbar_title)
    TextView title;
    @BindView(R.id.tv_right)
    TextView tvRight;
    private TimePickerView timePickerView;
    private int current;
    @BindView(R.id.tv_start_time)
    TextView tvStartTime;
    @BindView(R.id.tv_end_time)
    TextView tvEndTime;
    @BindView(R.id.tv_tag)
    TextView tvTag;
    @BindView(R.id.tv_notice)
    TextView tvNotice;
    @BindView(R.id.et_title)
    EditText etTitle;
    private ActionSheetDialog actionSheetDialog;
    private String currentDate;
    @Inject
    MainViewModel mainViewModel;
    @Inject
    ScheduleViewModel scheduleViewModel;

    List<Dictionary> dictionaries=new ArrayList<>();

    private Schedule schedule=new Schedule();
    private int[] times=new int[]{5,10,20,30,60};

    public static EditScheduleFragment newInstance() {
        return new EditScheduleFragment();
        // Required empty public constructor
    }
    @Override
    public void onLoadRetry() {
        mainViewModel.getDictionary(Constant.DICTIONARY_dayScheduleTag)
                .observe(this, listBaseResponse -> {
                    dictionaries=listBaseResponse.getData();

                });
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        onLoadRetry();
    }
    @Override
    public int layoutId() {
        return R.layout.fragment_edit_schedule;
    }

    @Override
    public boolean initView() {
        getDigger().inject(this);
        title.setText(getString(R.string.edit_schedule_title1));
        tvRight.setText(getString(R.string.edit_schedule_save));
        currentDate=EditScheduleFragmentArgs.fromBundle(getArguments()).getDate();
        timePickerView=getTimePickerView((date, v) -> {
            String d=DateUtils.dateFormat(date,"HH:mm");
            if(current==0){

                schedule.setStartTime(currentDate+" "+d+":00");
                tvStartTime.setText(d);
            }else {
                schedule.setEndTime(currentDate+" "+d+":00");
                tvEndTime.setText(d);
            }
        }).setType(new boolean[]{false, false, false, true, true, false}).build();
        return true;
    }


    @OnClick(R.id.iv_back)
    public void backClick() {
        NavHostFragment.findNavController(this).navigateUp();
    }
    @OnClick({R.id.ll_start,R.id.ll_end})
    public void pick(View view){

        switch (view.getId()){
            case R.id.ll_start:
                current=0;
                break;
            case R.id.ll_end:
                current=1;
                break;
        }
        timePickerView.show();
    }
    @OnClick(R.id.tv_right)
    public void right(){
       if(TextUtils.isEmpty( etTitle.getText().toString().trim())){
           showToast("请输入日程标题");
           return;
       }else {
           schedule.setContext(etTitle.getText().toString().trim());
       }
        if(TextUtils.isEmpty( tvTag.getText().toString().trim())){
            showToast("请选择标签");
            return;
        }
        if(TextUtils.isEmpty( tvStartTime.getText().toString().trim())){
            showToast("请选择开始时间");
            return;
        }
        if(TextUtils.isEmpty( tvEndTime.getText().toString().trim())){
            showToast("请选择结束时间");
            return;
        }
        if(TextUtils.isEmpty( tvNotice.getText().toString().trim())){
            showToast("请选择到期提醒");
            return;
        }
        scheduleViewModel.addSchedule(schedule)
                .observe(this, scheduleBaseResponse -> {
                    backClick();
                });
    }
    List<String> list;
    @OnClick({R.id.ll_tag,R.id.ll_notice})
    public void click(View view){
        list=new ArrayList<>();
        switch (view.getId()){
            case R.id.ll_tag:

                for(Dictionary dictionary:dictionaries){

                    list.add(dictionary.getName());
                }

                break;
            case R.id.ll_notice:
                list= Arrays.asList(getResources().getStringArray(R.array.schedule_notice));

                break;
        }
        String[] s=new String[]{};
        actionSheetDialog=new ActionSheetDialog(getContext(),list.toArray(s),null);
        actionSheetDialog.isTitleShow(false);
        actionSheetDialog.setOnOperItemClickL((parent, view1, position, id) -> {
            switch (view.getId()){
                case R.id.ll_tag:
                    schedule.setTag(dictionaries.get(position).getCode());
                    tvTag.setText(list.get(position));

                    break;
                case R.id.ll_notice:

                    tvNotice.setText(list.get(position));
                    schedule.setBeforeMinuteRemind(times[position]);
                    break;
            }
            actionSheetDialog.dismiss();
        });
        actionSheetDialog.show();
    }
}
